//
//  FeedBackVC.m
//  New_Runway
//
//  Created by mack on 2018/3/28.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "FeedbackVC.h"

@interface FeedBackVC ()<UITextViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (weak, nonatomic) IBOutlet UILabel *wordNum;
@property (weak, nonatomic) IBOutlet UIButton *pictureBtn;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (nonatomic, copy) NSString *contet;
@property (nonatomic, strong) NSData *data;

@end

@implementation FeedBackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contet = @"";
    self.data = [NSData data];
    self.navigationBarView.titleLabel.text = @"意见反馈";
    self.view.backgroundColor = kColorWhite();
    self.topHeight.constant = self.navH + 20;
    self.textview.tintColor = MAIN_NAV_COLOR();
    self.submitBtn.layer.masksToBounds = YES;
    self.submitBtn.layer.cornerRadius = 5;
    self.textview.delegate = self;
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    
    if([textView.text isEqualToString:@"请输入反馈内容"]){
        textView.text = @"";
        self.contet = @"";
    }
    
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length == 0) {
        textView.text = @"请输入反馈内容";
    }
}
-(void)textViewDidChange:(UITextView *)textView{
    
    if ([textView.text length] > 50) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 50)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        return;
    }
    
    self.contet = textView.text;
    
    self.wordNum.text = [NSString stringWithFormat:@"(%lu/50)",(unsigned long)textView.text.length];
    
}
- (IBAction)choosePicture:(id)sender {
    [self editPeopleMessage];
}
-(void)editPeopleMessage{
    
    UIActionSheet *sheet;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil];
    }
    else {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    
    
    [sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (buttonIndex) {
                
            case 0: //相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1: //相册
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            case 2:
                return;
                break;
        }
    }
    else {
        switch (buttonIndex) {
            case 0:
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                
                break;
            case 1:
                return;
                break;
                
        }
}
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:^{}];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.pictureBtn setImage:image forState:UIControlStateNormal];
    NSData *data = [UIImage reSizeImageData:image maxImageSize:375 maxSizeWithKB:1024];
 
    self.data = data;
    
}

- (IBAction)submitFeedBack:(id)sender {
    if ([self.contet StringIsNullOrEmpty:self.contet] == YES) {
        [ShowMessage showHintMessage:@"请输入反馈内容!" duration:2.0 withRemindId:remindWarning];

        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,feedBackApi];
    NSDictionary *params = @{
                             @"feedbackContent":self.contet,
                             @"userId":[NRWCarUser sharedUser].userId
                             };
    [YQNetworking uploadFileWithUrl:url params:params fileData:self.data type:@"jpg" name:@"uploadFile" mimeType:@"image/*" progressBlock:^(int64_t bytesWritten, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            
            [ShowMessage showHintMessage:@"提交成功!" duration:2.0 withRemindId:remindSuccess];

            [self.navigationController popViewControllerAnimated:YES];

        }else{
            [ShowMessage showHintMessage:response[@"msg"] duration:2.0 withRemindId:remindFail];

        }
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败！" duration:2.0 withRemindId:remindFail];

    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textview resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
